﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedComponents;

namespace OurLeague.Object
{
    public class Dayweek
    {
        #region Fields

        String _id;
        DateTime _date;

        public static List<Dayweek> Dayweeks = new List<Dayweek>();

        #endregion

        #region Constructors

        public Dayweek()
        {
            _id = "";
            _date = new DateTime();
        }
        
        #endregion

        #region Public Methods

        public void delete()
        {
            MySqlQueryHelper.deleteQuery(MySqlManager.Connection, "Days", "dayId", new String[] { Id });
            Dayweeks.Remove(this);
            Match.removeAll(this);
        }

        public void save()
        {
            MySqlRecord updateRecord = new MySqlRecord();

            updateRecord["date"] = Date;

            if (!Dayweeks.Exists(dw => dw.Id == Id))
            {
                Dayweeks.Add(this);
                Id = MySqlQueryHelper.saveQuery(MySqlManager.Connection, "Days", null, updateRecord).ToString();
            } 
            else
                MySqlQueryHelper.saveQuery(MySqlManager.Connection, "Days", "dayId=" + Id, updateRecord);
            
            sorterDayweeks();
        }

        /*
        
        public void deleteEvent(String a) { }
        public void deleteMatch(String a) { }
        public void deleteFormation(String a) { }
        public void update() { }

        public int scoredGoals(String a)
        {
            return 0;
        }

        public int concededGoals(String a)
        {
            return 0;
        }

        public String home(String a)
        {
            return null;
        }

        public bool findMatch(String a)
        {
            return false;
        }

        public bool findDayweek(String a)
        {
            return false;
        }

        
        public void setLocalFormation(List<String> a, List<String> b, String c, String d) { }

        public void setGuestFormation(List<String> a, List<String> b, String c, String d) { }

        public void addEvent(String a, int b, String c, String d) { }

        public void insertMatch(String a, String b, String c, int d, String e, String f) { }

        public void editMatch(String a, String b, String c, int d, String e, String f) { }

        public void editEvent(String a, String b, String c, int d, String e) { }

        public List<String> viewMatch()
        {
            return null;
        }

        public long minutesPlayed(String a)
        {
            return 0;
        }
        */

        #endregion

        #region Static Methods

        public static Dayweek load(String idDayweek)
        {
            Dayweek dayweek = Dayweeks.Count > 0 ? Dayweeks.Find(d => d._id == idDayweek) : null;
            
            if (dayweek == null)
            {
                var queryResult = MySqlQueryHelper.selectQuery(MySqlManager.Connection, "Days", null, "dayId='" + idDayweek + "'");
                dayweek = addNewDayweek(queryResult[0]);

                sorterDayweeks();
            }
            Dayweeks.OrderBy(dw => dw.Date);
            return dayweek;
        }

        private static void sorterDayweeks()
        {
            Dayweeks.OrderBy(dw => dw.Date);
            /*
            Dayweeks.Sort(delegate(Dayweek dw1, Dayweek dw2)
            {
                return dw1.Date.CompareTo(dw2.Date);
            });
            */
        }
        
        private static Dayweek addNewDayweek(MySqlRecord dayweekRecord)
        {
            Dayweek dayweek = new Dayweek();

            dayweek._date = DateTime.Parse(dayweekRecord["date"].ToString());
            dayweek._id = dayweekRecord["dayId"].ToString(); ;

            Dayweeks.Add(dayweek);

            return dayweek;
        }

        public static void loadAll()
        {
            var queryResult = MySqlQueryHelper.selectQuery(MySqlManager.Connection, "Days", null,null);

            foreach (MySqlRecord dayweekRecord in queryResult)
            {
                if (Dayweeks.Find(dw => dw.Id == dayweekRecord["dayId"].ToString()) == null)
                    addNewDayweek(dayweekRecord);
            }

            sorterDayweeks();
        }

        #endregion

        #region Properties

        public System.Drawing.Image Status
        {
            get { return (Match.Matches.Where(m => m.Dayweek.Id == Id).Count() > 0 ? OurLeague.Properties.Resources.tick : OurLeague.Properties.Resources.cross); }
        }

        public String Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public DateTime Date
        {
            get { return _date; }
            set { _date = value; }
        }

        public int DayweekNumber
        {
            get { return Dayweeks.FindIndex(D => D == this)+1; }
        }

        #endregion
    }
}
